package gui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Gui extends JFrame implements ActionListener{ 

	JPanel p1 = new JPanel();
	JLabel tieude = new JLabel("Phuong trinh bac hai - Director:Phantom-20/2/2014");
	JLabel l1 = new JLabel("He so a");
	JLabel l2 = new JLabel("He so b");
	JLabel l3 = new JLabel("He so c");
	JLabel result = new JLabel("Result");
	JTextField t1= new JTextField(10);
	JTextField t2= new JTextField(10);
	JTextField t3= new JTextField(10);
	JTextField t4= new JTextField(10);
	JButton solve= new JButton("Solve");
	JButton reset= new JButton("Reset");
	JButton exit= new JButton("Exit");
	String s4,s5,s6,s7;
	private double a ,b,c,delta,x1,x2;
	
	public Gui(){ //Class tao giao dien 


		setSize(500,300);
		setLayout(null);

		p1.setSize(500,1000);
		p1.setLayout(null);
		add(p1);
		p1.add(t1);
		p1.add(t2);
		p1.add(t3);
		p1.add(t4);
		p1.add(result);
		p1.add(solve);
		p1.add(reset);
		p1.add(exit);
		p1.add(l1);
		p1.add(l2);
		p1.add(l3);
		p1.add(tieude);
		tieude.setBounds(190,60,140,30);
		tieude.setBackground(Color.red);
		tieude.setOpaque(true);
		l1.setBounds(90,60,140,130);
		t1.setBounds(150,115,30,20);
		l2.setBounds(180,60,140,130);
		t2.setBounds(240,115,30,20);
		l3.setBounds(270,60,140,130);
		t3.setBounds(330,115,30,20);
		result.setBounds(90,105,140,130);
		t4.setBounds(150,155,200,20);

		solve.setBounds(100,200,100,30);
		reset.setBounds(220,200,80,30);
		exit.setBounds(320,200,80,30);
		solve.addActionListener(this);
		reset.addActionListener(this);
		exit.addActionListener(this);
		setVisible(true);
	}
	public static void main(String[] args) { // ham main
		Gui g = new Gui();
	}
	public void PTB1(){
		if(b==0){
			if(c==0) t4.setText("Phuong trinh co vo so nghiem");
			else t4.setText("Phuong trinh vo nghiem");
		}
		else{
			
			x1=-c/b;

			s4=String.valueOf(x1);
			t4.setText(s4);
		}
	}
	public void PTB2(){
		delta = b*b-4*a*c;
		if(delta<0) t4.setText("Phuong trinh vo nghiem");
		else{
			if(delta==0) {
				x1=-b/(2*a);
				s5=String.valueOf(x1);
				t4.setText(s5);
			}
			else{
				x1=-b-Math.sqrt(delta)/(2*a);
				x2=-b+Math.sqrt(delta)/(2*a);			
				s6=String.valueOf(x1);
				s7=String.valueOf(x2);
				t4.setText("x1 = "+s6 +"  x2= "+ s7);
				}
		}
	}
	@Override
	public void actionPerformed(ActionEvent e) { //Xu ly cac su kien trong class
		
		try{
		if(e.getSource()==solve){
			String s1,s2,s3;
			s1=t1.getText();
			s2=t2.getText();
			s3=t3.getText();
			a=Double.valueOf(s1);
			b=Double.valueOf(s2);
			c=Double.valueOf(s3);
			
			if(a==0){
				PTB1();
			}
			else{
				PTB2();
			}
		}
		}catch(Exception L){
			JOptionPane.showMessageDialog(null,"sai");
			t1.setText("");
			t2.setText("");
			t3.setText("");
		}
		if(e.getSource()==reset){
			t1.setText("");
			t2.setText("");
			t3.setText("");
			t4.setText("");
		}
		if(e.getSource()==exit){
			System.exit(0);
			this.dispose();
		}
	}
}

